#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

//    bool ret = m_zipCompress.syncCompressDir("/data/home/UOS/untitled.zip", "/data/home/UOS/717");
//    qDebug() << ret ;

//    QStringList ss = m_zipCompress.syncExtractDir("/data/home/UOS/untitled.zip", "/data/home/UOS/qt");
//    if (ss.count() > 0) {
//        QMessageBox::information(this, "提示", "解压成功！");
//    } else {
//        QMessageBox::information(this, "提示", "解压失败！");
//    }


//    bool b = m_zipCompress.compressDirsWithPassword("E:/Desktop/qt.zip", "E:/Desktop/test/", "123456");
//    if (b) {
//        QMessageBox::information(this, "提示", "压缩成功！");
//    } else {
//        QMessageBox::information(this, "提示", "压缩失败！");
//    }

//    QStringList l;
//    l.append("/home/UOS/untitled/crollbox.cpp");
//    l.append("/home/UOS/untitled/selectdtdialog.h");
//    l.append("/home/UOS/untitled/widget.h");
//    l.append("/home/UOS/untitled/widget.ui");
//    bool b = m_zipCompress.compressFilesWithPassword("/data/home/UOS/qt.zip", l, "123456");
//    if (b) {
//        QMessageBox::information(this, "提示", "压缩成功！");
//    } else {
//        QMessageBox::information(this, "提示", "压缩失败！");
//    }

//    bool b = m_zipCompress.extractDirWithPassword("E:/Desktop/qt.zip", "E:/Desktop/", "123456");
//    if (b) {
//        QMessageBox::information(this, "提示", "解压成功！");
//    } else {
//        QMessageBox::information(this, "提示", "解压失败！");
//    }


//    QString folder = QFileDialog::getExistingDirectory(
//                nullptr,
//                "select folder",
//                QString(),
//                QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
//                );

//    if (folder.isEmpty()) {
//        return;
//    }
//    QFileInfo fileinfo(folder);
//    QString zipPath = fileinfo.path()+"/"+ fileinfo.fileName()+".zip";
//    m_zipCompress.compressDirsWithPassword(zipPath, folder, password);

    mWorkerThread = new WorkerThread(this);

    connect(mWorkerThread, &WorkerThread::signalCompressionProgress,
            this, &Widget::slotsCompressionProgress, Qt::ConnectionType::QueuedConnection);
    connect(mWorkerThread, &WorkerThread::signalFileProgress,
            this, &Widget::slotsFileProgress, Qt::ConnectionType::QueuedConnection);
    connect(mWorkerThread, &WorkerThread::signalThreadFinished, this, &Widget::slotsDirinish, Qt::ConnectionType::QueuedConnection);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_c_clicked()
{
    QString folder = QFileDialog::getExistingDirectory(
                nullptr,
                "select folder",
                QString(),
                QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
                );

    if (folder.isEmpty()) {
        return;
    }

    ui->pushButton_c->setEnabled(false);
    ui->pushButton_e->setEnabled(false);
    ui->pushButton_c_2->setEnabled(false);
    QFileInfo fileinfo(folder);
    QString zipPath = fileinfo.path()+"/"+ fileinfo.fileName()+".zip";

    mWorkerThread->setOperatorPath("1", zipPath, folder, password);
    mWorkerThread->start();
}
void Widget::on_pushButton_e_clicked()
{

    QString fileName = QFileDialog::getOpenFileName(
                this,
                "Select ZIP File",
                QString(),
                "ZIP Files (*.zip);;All Files (*)");

    if (fileName.isEmpty()) {
        return;

    }

    ui->pushButton_c->setEnabled(false);
    ui->pushButton_e->setEnabled(false);
    ui->pushButton_c_2->setEnabled(false);
    QFileInfo fileinfo(fileName);


    mWorkerThread->setOperatorPath("2", fileName, fileinfo.path(), password);
    mWorkerThread->start();
}

void Widget::on_pushButton_c_2_clicked()
{
    QStringList files = QFileDialog::getOpenFileNames(this, tr("选择文件"), QString(), "All Files (*)");
    if (files.isEmpty()) {
        return;
    }

    QString name = extractLastDirName(files.first());
    if (name.isEmpty()) {
        name = "ZIP_FILE";
    }

    ui->pushButton_c->setEnabled(false);
    ui->pushButton_e->setEnabled(false);
    ui->pushButton_c_2->setEnabled(false);

    QFileInfo fileinfo(files.first());
    QString zipPath = fileinfo.path() + "/" + name + ".zip";
    mWorkerThread->setOperatorPath("3", zipPath, QString(), password, files);
    mWorkerThread->start();
}

void Widget::slotsDirinish(QString operateType, bool status)
{
    QString text = "压缩成功";

    if ("1" == operateType) {
        if (status) {
            text = "压缩成功";
        } else {
            text = "压缩失败";
        }


    } else if ("2" == operateType) {
        if (status) {
            text = "解压成功";
        } else {
            text = "解压失败";
        }
    }

    QMessageBox::information(this, "提示", text);

    ui->pushButton_c->setEnabled(true);
    ui->pushButton_e->setEnabled(true);
    ui->pushButton_c_2->setEnabled(true);
}

void Widget::slotsCompressionProgress(qreal progress, QString operateType, QString filePath)
{

    QString text = QString::number(progress) + "%"; // 转换为字符串，保留两位小数
    ui->progressBar->setValue(progress); // 设置进度条值
    ui->progressBar->setFormat(text); // 设置格式化文本（虽然这不是通常的做法）

    //ui->progressBar->setValue(progress);

    QString str = QString("%1  %2").arg(operateType=="1" ? "压缩" : "解压").arg(filePath);
    //ui->label_name->setText(str);
}

void Widget::slotsFileProgress(qreal progress, QString filePath)
{
    ui->progressBarFile->setValue(progress);

    QString str = QString("  %1").arg(filePath);
    ui->label_name_file->setText(str);
}

QString Widget::extractLastDirName(const QString &filePath)
{
    QStringList list = filePath.split("/");
    if (list.count() < 2) {
        return "";
    }
    QString lastDirName = list[list.count() - 2];

    if (lastDirName.isEmpty() || lastDirName.contains(":")) {
        return "";
    }

    return lastDirName;
}




WorkerThread::WorkerThread(QObject *parent) : QThread(parent)
{
    connect(&m_zipCompress, &compressionTool::zipCompress::signalCompressionProgress,
            this, &WorkerThread::signalCompressionProgress);

    connect(&m_zipCompress, &compressionTool::zipCompress::signalFileProgress,
            this, &WorkerThread::signalFileProgress);

    connect(&m_zipCompress, &compressionTool::zipCompress::signalCompressDirinish, [&] (bool status) {
        emit signalThreadFinished("1", status);
    });
    connect(&m_zipCompress, &compressionTool::zipCompress::signalExtractDirFinish, [&] (bool status) {
        emit signalThreadFinished("2", status);
    });
}

WorkerThread::~WorkerThread()
{

}

void WorkerThread::setOperatorPath(const QString &mOperator, const QString &zipPath, const QString &path, const QString &password, const QStringList &files)
{
    this->mOperator = mOperator;
    this->mZipPath = zipPath;
    this->mPath = path;
    this->mPassword = password;
    this->mFiles = files;
}

void WorkerThread::run()
{
    if ("1" == mOperator) { // 压缩文件夹
        m_zipCompress.compressDirsWithPassword(mZipPath, mPath, mPassword);

    } else if ("2" == mOperator) {  // 解压缩
        m_zipCompress.extractDirWithPassword(mZipPath, mPath, mPassword);

    } else if ("3") {   // 压缩文件
        m_zipCompress.compressFilesWithPassword(mZipPath, mFiles, mPassword);
    }
}


